package com.luge.model.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luge.base.Base;
import com.luge.heart.MainActivity;
import com.luge.heart.R;
import com.luge.model.SongItem;
import com.luge.model.download.DownLoad.DownLoadCallBack;
import com.luge.model.download.DownLoadManager;
import com.luge.model.download.ImageManager;
import com.luge.utils.Helper;

public class SongItemView extends RelativeLayout implements ReleaseInterface {
	private static final int LEFT_IMG_ID = 1;
	private static final int TITLE_ID = 2;
	private static final int DURATION_ID = 3;

	private TextView nameTxt;
	private TextView introTxt;
	private ImageView icon;
	private int width;
	private Bitmap bitmap;

	public SongItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public SongItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SongItemView(Context context) {
		super(context);
		init();
	}

	private void init() {
		width = Helper.getDisplayWidth(getContext());
		setBackgroundResource(R.drawable.song_item_backgroud);
		initLeftImage();
		initLayout();
	}

	private void initLayout() {
		LinearLayout mLayout = new LinearLayout(getContext());
		RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		mParams.setMargins(15, 0, 0, 0);
		mParams.addRule(RelativeLayout.RIGHT_OF, LEFT_IMG_ID);
		mParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
		mLayout.setOrientation(LinearLayout.VERTICAL);
		addView(mLayout, mParams);
		initIntroTxt(mLayout);
		initNameTxt(mLayout);
	}

	private void initIntroTxt(LinearLayout mLayout) {
		introTxt = new TextView(getContext());
		introTxt.setId(DURATION_ID);
		LinearLayout.LayoutParams durationParams = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		introTxt.setTextSize(13);
		introTxt.setTextColor(getResources().getColor(R.color.white));
		introTxt.setSingleLine(true);
		introTxt.setEllipsize(TruncateAt.END);
		mLayout.addView(introTxt, durationParams);
	}

	private void initNameTxt(LinearLayout mLayout) {
		nameTxt = new TextView(getContext());
		nameTxt.setId(TITLE_ID);
		LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		nameTxt.setTextSize(12);
		nameTxt.setTextColor(getResources().getColor(R.color.white));
		nameTxt.setSingleLine(true);
		nameTxt.setEllipsize(TruncateAt.END);
		mLayout.addView(nameTxt, titleParams);
	}

	private void initLeftImage() {
		icon = new CustomImageView(getContext());
		icon.setId(LEFT_IMG_ID);
		int w = width / 7;
		RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(
				w, w);
		leftParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
				RelativeLayout.TRUE);
		icon.setScaleType(ScaleType.CENTER_CROP);
		icon.setImageResource(R.drawable.singer_default);
		addView(icon, leftParams);
	}

	public void setInfo(SongItem item) {
		if (MainActivity.songId == null) {
			setBackgroundResource(R.drawable.song_item_bg);
		} else if (MainActivity.songId.equals(item.getId())) {
			setBackgroundResource(R.drawable.song_item_bg_press);
		} else {
			setBackgroundResource(R.drawable.song_item_bg);
		}
		nameTxt.setText(item.getSinger().trim() + "-" + item.getName().trim());
		introTxt.setText(item.getInfo().trim());
		icon.setImageResource(R.drawable.singer_default);
		final String imgUrl = item.getImgUrl();
		final String filePath = ImageManager.getFilePath(Base.cache_path,
				imgUrl);
		DownLoadCallBack callBack = new DownLoadCallBack() {
			@Override
			public void success(String urlPath) {
				try {
					if (imgUrl.equals(urlPath)) {
						bitmap = BitmapFactory.decodeFile(filePath);
						icon.setImageBitmap(bitmap);
					}
				} catch (OutOfMemoryError e) {
				}
			}

			@Override
			public void progress(int progress) {
			}

			@Override
			public void fail() {
				icon.setImageResource(R.drawable.singer_default);
			}
		};
		DownLoadManager.getInstance().add(imgUrl, filePath, callBack);
	}

	@Override
	public void release() {
		if (bitmap != null) {
			bitmap.recycle();
		}
	}

}
